<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/view/common/taglibs.jsp"%>
<html>
	<head>
		<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
		<title>发票申请-金融风险管理系统</title>
		<script src="${contextPath}/scripts/jquery/jquery-1.8.3.min.js" type="text/javascript"></script>
		<script src="${contextPath}/scripts/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
		<script src="${contextPath}/scripts/common.js" type="text/javascript"></script>
		<script src="${contextPath}/scripts/scriptvalidator.js" type="text/javascript"></script>
		<link href="${contextPath}/styles/default.css" rel="stylesheet" type="text/css" />
		<script language='javascript'>
		String.prototype.replaceAll =function(s1,s2) {

		    return this.replace(new RegExp(s1,"gm"),s2);

		};
		
		function jumpPage(page) {
			var form1 = document.form1;
			form1.action = "<s:property value='contextPath'/>/cs/add.do?currentPage=" + page;
			form1.submit();
		}
		  
	  $(document).ready(function(){
			$("[type=button][name='btn_submit']").click(function(e){
				$("#operate").val(e.target.id);
				$("#form1").action ="<s:property value='contextPath'/>/cs/create.do";
				$("#form1").submit();
			}); 
			
			});
	  
	  //添加行
	  function add_tr(){ 
		  var finCount=$("#fin").get(0).options.length;
		  if(finCount==0){
			  alert("没有可预收的金融机构");
		  }else{
			  var finId=$("#hidtr").find("td").eq(1).find("select").eq(0).val();
			  var next=getNextMonth(finId);
			  nextAry=next.split("_");
			  
			  var count= $("#tab").find("tr:last").find("td:first").text();
			  var v= $("#hidtr").clone(true);
			  v.appendTo("#tab");
			  v.show();
			  $("#tab").find("tr:last").find("td:first").text((count*1+1)); 
			  
			  v.find("td").eq(2).find("input").eq(0).val(nextAry[0]);
			  v.find("td").eq(3).find("input").eq(0).val(nextAry[1]);
			  
			  v.find("td").eq(2).find("input").eq(0).attr("datatype","Require|Double"); 
			  v.find("td").eq(2).find("input").eq(0).attr("msg","金额不能为空|格式不正确"); 
			  v.find("td").eq(3).find("input").eq(0).attr("datatype","Require");
			  v.find("td").eq(3).find("input").eq(0).attr("msg","月份不能为空");
			  
		  }

	  }
	  
	//删除行
	function del_tr(el){
		$(el).parent().parent().remove();
	}
	//删除行
	function del_tr_db(el){
		var id=$(el).parent().parent().find("td:last").text();
		var l=$(el).parent().parent().find('td').eq(4).find("input").length;
		if(l==1){//月份
			$.ajax({
				  async:false,
				  type: 'POST',
				  url: "<s:property value='contextPath'/>/mf/apply/delBillOption.do?id="+id,
				  dataType: "json",
				  success: function(data) {
						  	f = data.flag;
						  	if(f){
						  		$(el).parent().parent().remove();
						  	}
						}		
					,
					error:function(data){
					}
				}); 
		}else if(l==2){//季度、半年
			var n=$(el).parent().parent().find('td').eq(4).find("input:last").attr("name");
			$.ajax({
				  async:false,
				  type: 'POST',
				  url: "<s:property value='contextPath'/>/mf/apply/delBillOptionByGroup.do?group="+n,
				  dataType: "json",
				  success: function(data) {
						  	f = data.flag;
						  	if(f){
						  		$("input[name='"+n+"']").parent().parent().remove();
						  	}
						}		
					,
					error:function(data){
					}
				}); 
		}
		
		
	}
	  
	//校验-月份
	function check_mon(el){
		var month=$(el).val();
		var finId=$(el).parent().parent().children('td').eq(1).children('select:first').val();
		var flag=check(finId,month);
		if(flag){
			$(el).parent().children('font').remove();
			$(el).after("<font color='red'>此月份重复预收</font>");
			//$(el).after("<p>此月份重复预收</p>");
		}else{
			$(el).parent().children('font').remove();
		}
	}
	//校验-金融机构
	function check_fin(el){
		var finId=$(el).val();
		var month=$(el).parent().parent().children('td').eq(3).children('input:first').val();
		if(check(finId,month)){
			$(el).parent().parent().children('td').eq(3).children('input:first').after("<font color='red'>此月份重复预收</font>");
		}else{
			$(el).parent().parent().children('td').eq(3).children('font').remove();
		}
		
	}
	//校验是否重复预收
	function check(finId,month){
		var count=0;
		$("select:visible[value="+finId+"]").each(function(i){
			var tr=$(this).parent().parent();
			var tmpMon=tr.children('td').eq(3).children('input:first').val();
			if(month==tmpMon)
				count++;
			});
		if(count>1){
			//重复,添加提示
			return true;
		}
		var f=false;
		$.ajax({
			  async:false,
			  type: 'POST',
			  url: "<s:property value='contextPath'/>/mf/apply/checkMonth.do?months="+month+"&financialId="+finId+"&shopId="+$("#shopId").val(),
			  dataType: "json",
			  success: function(data) {
					  	f = data.flag;
					  	if(f){
					  		//重复,添加提示
					  		return true;
					  	}
					}		
				,
				error:function(data){
				}
			});
		return f;
	  }
	  
	  //获取下一个月份
	  function getNextMonth(finId){
		  var months='';
		  $("select:visible[value="+finId+"]").each(function(i){
			  var tr=$(this).parent().parent();
			  var tmpMon=tr.children('td').eq(3).children('input:first').val();
			  months=months+tmpMon+',';
			  });
		  if(months.length>1)
			  months=months.substring(0,months.length-1);
		  var f='';
			$.ajax({
				  async:false,
				  type: 'POST',
				  url: "<s:property value='contextPath'/>/mf/apply/findNextMonth.do?months="+months+"&financialId="+finId+"&shopId="+$("#shopId").val(),
				  dataType: "json",
				  success: function(data) {
						  	f = data.data;
						  	//alert(f);
						  	return f;
						}		
					,
					error:function(data){
					}
				}); 
			return f;
	  }
	  
	  //封装保存数据
	  function build(){
		  var editData='',newData='';
		  var editTrs=$(".data_tr:visible");
		  var newTrs=$(".data_new_tr:visible");
		  for(var i=0;i<editTrs.size();i++){
			  var tr=editTrs[i];
			  editData+=$.trim($(tr).find("td:last").text());
			  if($(tr).children('td').eq(2).children('input:first').length==1){
				  editData+="_"+$(tr).children('td').eq(2).children('input:first').val();  
			  }else{
				  editData+="_"+$(tr).children('td').eq(2).text();
			  }
			  var remark='';
			  if($(tr).children('td').eq(4).children('input:first').length==1){
				  remark=$(tr).children('td').eq(4).children('input:first').val();
			  }else{
				  remark=$(tr).children('td').eq(4).text();
			  }
		  	  remark=remark.replaceAll("_","");
		  	  remark=remark.replaceAll(",","");
		  		  
		  	  editData+="_"+remark;
			  if(i!=editTrs.size()-1)
			  	editData+=",";
		  }
		  for(var i=0;i<newTrs.size();i++){
			  var tr=newTrs[i];
			  newData+=$(tr).children('td').eq(1).children('select:first').val();
			  var money=$(tr).children('td').eq(2).children('input:first').val();
			  if(money==null||money=='')
				  money=0;
			  newData+="_"+money;
			  newData+="_"+$(tr).children('td').eq(3).children('input:first').val();
			  var remark=$(tr).children('td').eq(4).children('input:first').val();
			  remark=remark.replaceAll("_","");
		  	  remark=remark.replaceAll(",","");
			  newData+="_"+remark;
			  if(i!=newTrs.size()-1)
				  newData+=",";
		  }
		  // alert(editData);
		  // alert(newData);
		  $("#editData").val(editData);
		  $("#newData").val(newData);
		  if($("font").length==0){
			  $("#form1").submit();
		  }
		  	  
	  }
	  
	  function display(id){
			window.open("<s:property value='contextPath'/>/mf/apply/billDisplay.do?id="+id, "newwindow", "height=800, width=800, toolbar =no, menubar=no, scrollbars=no, resizable=no, location=no, status=no");
		}
	  
	  function chooseFin(){
		  var id=$("#bill_id").val();
		  window.showModalDialog("<s:property value='contextPath'/>/mf/apply/chooseFin.do?entity.id="+id,window,"dialogheight:480px;dialogwidth:680px;status:no;toolbar:no;menubar:no;scrollbars:no;resizable:no");  
	  }
	  
	  function chooseFinCallBack(type,finId,finName){
		  //alert("## "+type+"_"+finId);
		  //ajax 调用后台，获得3个或6个月份   和金额.后台存数据库
		  var f='';
			$.ajax({
				  async:false,
				  type: 'POST',
				  url: "<s:property value='contextPath'/>/mf/apply/addAdvanceMonth.do?type="+type+"&financialId="+finId+"&shopId="+$("#shopId").val()+"&entity.id="+$("#bill_id").val(),
				  dataType: "json",
				  success: function(data) {
						  	f = data.data;
						  	//alert(f);
						  	return f;
						}		
					,
					error:function(data){
					}
				}); 
			if(f=="0"){
				if(type=="1"){
					alert("您所选金融机构可收取的月份已不足一个季度(三个月),您可以续签协议");	
				}else{
					alert("您所选金融机构可收取的月份已不足半年(六个月),您可以续签协议");
				}
				
			}else{
				var ary_data=f.split("|");
				if(type=="1"){
					add_sh_tr(ary_data[0],ary_data[1],finName,3,ary_data[2]);	
				}else{
					add_sh_tr(ary_data[0],ary_data[1],finName,6,ary_data[2]);
				}
				
			}
			
			//return f;
		  //根据获得的月份和金额生成行
	  }
	  
	//添加季度/半年 行
	  function add_sh_tr(mons,d,finName,rows,random){
		//alert("#"+mons+"#"+d+"#"+finName+"#"+rows);
		 var mons_ary=mons.split(",");
		 for(var i=0;i<mons_ary.length;i++){
			 var mon_id=mons_ary[i].split("_");
			 var mon=mon_id[0];
			 var id=mon_id[1];
			 //if(i==0){
				  var count= $("#tab").find("tr:last").find("td:first").text();
				  var v= $("#hidtr_sh").clone(true);
				  v.appendTo("#tab");
				  //v.find("td").eq(5).attr("rowspan",rows);
				  v.show();
				  v.find("td").eq(1).text(finName);//设置金融机构名称
				  $("#tab").find("tr:last").find("td:last").text(id);//option ID 
				  $("#tab").find("tr:last").find("td:first").text((count*1+1)); 
				  v.find("td").eq(2).find("input").eq(0).val(d);
				  v.find("td").eq(3).text(mon);
				  v.find("td").eq(4).find("input:last").attr("name",random);
				  v.find("td").eq(2).find("input").eq(0).attr("datatype","Require|Double"); 
				  v.find("td").eq(2).find("input").eq(0).attr("msg","金额不能为空|格式不正确"); 
			/*  }else{
				 count= $("#tab").find("tr:last").find("td:first").text();
				  var v= $("#hidtr_nodel").clone(true);
				  v.appendTo("#tab");
				  v.show();
				  v.find("td").eq(1).text(finName);//设置金融机构名称
				  $("#tab").find("tr:last").find("td:first").text((count*1+1)); 
				  v.find("td").eq(2).find("input").eq(0).val(d);
				  v.find("td").eq(3).text(mon);
				  v.find("td").eq(2).find("input").eq(0).attr("datatype","Require|Double"); 
				  v.find("td").eq(2).find("input").eq(0).attr("msg","金额不能为空|格式不正确");
			 } */
		 }

	  }
		</script>
	</head>
	<body  leftmargin='8' topmargin='8'>
	<div class="navigation">
	    <span><a href="javascript:window.history.back();"><img src="${contextPath}/images/back_nav_btn.png" /></a>
	    <a href="javascript:window.history.back();">返回</a>
	    <a href="javascript:window.history.forward();"><img src="${contextPath}/images/go_nav_btn.png" /></a>
	     <a href="javascript:window.history.forward();">前进</a>
	    <a href="javascript:window.location.reload();"><img src="${contextPath}/images/refresh_nav_btn.png" /></a>
	     <a href="javascript:window.location.reload();">刷新</a>
 	    </span>管理费&gt;&gt;发票申请
    
  	</div>
		<div class="xt_main_right_center">
		<form id="form1" name="form1" action="billUpdate.do" method="post"  onSubmit="return Validator.Validate(this,3)">
			<table width="100%" border="0" cellpadding="1" cellspacing="1">
			<tr>
				<td class="left-td-width-bg">
					店名：
				</td>
				<td width="30%" class="bline" align="left">
					<s:property value="bill.shop.shopName"/>
				</td>
				<td class="left-td-width-bg">
					驻店顾问：
				</td>
				<td width="25%" class="bline" align="left">
					<s:property value="findUserShopByShopId(bill.shop.id)"/>
				</td>
				<td width="20%" class="bline" align="left">
					<input type="button" onclick="add_tr()" value="新增月份预收" />&nbsp;
					<input type="button" onclick="chooseFin()" value="新增季度/半年预收" />&nbsp;
				</td>
			</tr>
		</table>
	
		<!-- 列表开始 -->	
		<%-- <s:if test="bill.options!=null"> --%>
			<br/>
			<table id="tab" width="100%" border="0" cellpadding="1" cellspacing="1" bgcolor="#CCCCCC">
			  <tr> 
			    <td width="4%" class="top-td-width-bg">序号</td>
			    <td width="9%" class="top-td-width-bg">银行</td>
			    <td width="10%" class="top-td-width-bg">应收金额</td>
			    <td width="20%" class="top-td-width-bg">收取月份</td>
			    <td width="10%" class="top-td-width-bg">备注</td>
			    <td width="7%" class="top-td-width-bg">操作</td>
			  </tr>
			  
			  <tr class="data_new_tr" id="hidtr" style="display: none" height="26" align="center" bgcolor="#FFFFFF" onmousemove="javascript:this.bgColor='#FCFDEE';" onmouseout="javascript:this.bgColor='#FFFFFF';">
					<td  class="down-center-td-width-bg">
						<s:property value="1"/>
					</td> 
					<td  class="down-center-td-width-bg">
					<s:if test="financials!=null">
						<s:select id="fin" list="financials" listKey="id" listValue="name" onchange="check_fin(this)" theme="simple"></s:select>
					</s:if>
					</td> 
					<td  class="down-center-td-width-bg">
						<input value="<s:property value="formatAmount(amount)"/>" />
					</td> 
					<td  class="down-left-td-width-bg">
						<!-- <input value="" onchange="check_mon(this)"/> -->
						<input class="Wdate" type="text" style="width: 100px" onchange="check_mon(this)" onFocus="WdatePicker({dateFmt:'yyyy-MM',minDate:'%y-%M'})" readonly="true" />
					</td> 
					<td  class="down-center-td-width-bg">
						<input value="<s:property value="remark"/>" />
					</td>
				 	<td  class="down-center-td-width-bg">
				 		<img src="${contextPath}/images/del.gif" alt="删除" onclick="del_tr(this);" width="16" height="13" border="0" />
					</td> 
					<td style="display: none"></td>
				</tr>
				<tr class="data_tr" id="hidtr_sh" style="display: none" height="26" align="center" bgcolor="#FFFFFF" onmousemove="javascript:this.bgColor='#FCFDEE';" onmouseout="javascript:this.bgColor='#FFFFFF';">
					<td  class="down-center-td-width-bg">
						<s:property value="1"/>
					</td> 
					<td  class="down-center-td-width-bg">
					<s:if test="financials!=null">
						<s:select id="fin" list="financials" listKey="id" listValue="name" onchange="check_fin(this)" theme="simple"></s:select>
					</s:if>
					</td> 
					<td  class="down-center-td-width-bg">
						<input value="<s:property value="formatAmount(amount)"/>" />
					</td> 
					<td  class="down-left-td-width-bg">
						<!-- <input value="" onchange="check_mon(this)"/> -->
						<input class="Wdate" type="text" style="width: 100px" onchange="check_mon(this)" onFocus="WdatePicker({dateFmt:'yyyy-MM',minDate:'%y-%M'})" readonly="true" />
					</td> 
					<td  class="down-center-td-width-bg">
						<input value="<s:property value="remark"/>" /><input type="hidden" name="" value="" />
					</td> 
				 	<td  class="down-center-td-width-bg">
				 		<img src="${contextPath}/images/del.gif" alt="删除" onclick="del_tr_db(this);" width="16" height="13" border="0" /><input type="hidden" />
					</td> 
					<td style="display: none"></td>
				</tr>
				<tr  class="data_tr" id="hidtr_nodel" style="display: none" height="26" align="center" bgcolor="#FFFFFF" onmousemove="javascript:this.bgColor='#FCFDEE';" onmouseout="javascript:this.bgColor='#FFFFFF';">
					<td  class="down-center-td-width-bg">
						<s:property value="1"/>
					</td> 
					<td  class="down-center-td-width-bg">
					<s:if test="financials!=null">
						<s:select id="fin" list="financials" listKey="id" listValue="name" onchange="check_fin(this)" theme="simple"></s:select>
					</s:if>
					</td> 
					<td  class="down-center-td-width-bg">
						<input value="<s:property value="formatAmount(amount)"/>" />
					</td> 
					<td  class="down-left-td-width-bg">
						<!-- <input value="" onchange="check_mon(this)"/> -->
						<input class="Wdate" type="text" style="width: 100px" onchange="check_mon(this)" onFocus="WdatePicker({dateFmt:'yyyy-MM',minDate:'%y-%M'})" readonly="true" />
					</td> 
					<td  class="down-center-td-width-bg">
						<input value="<s:property value="remark"/>" />
					</td>
					<td style="display: none"></td>
				</tr>
				
			 <s:iterator value="bill.options" status="stx" >
				<tr class="data_tr"  height="26" align="center" bgcolor="#FFFFFF" onmousemove="javascript:this.bgColor='#FCFDEE';" onmouseout="javascript:this.bgColor='#FFFFFF';">
					<td  class="down-center-td-width-bg">
						<s:property value="#stx.index+1"/>
					</td> 
					<td  class="down-center-td-width-bg">
						<s:property value="financial.name"/>
					</td> 
					<td  class="down-center-td-width-bg">
						<s:if test="operate==0"><input value="<s:property value="amount"/>" datatype="Require|Double" msg="金额不能为空|格式不正确" /></s:if>
						<s:else><s:property value="amount"/></s:else>
					</td> 
					<td  class="down-left-td-width-bg">
						<s:if test="operate==2"><a href="javascript:display(<s:property value="id"/>)" style="color:red"><s:property value="month"/>(预收)</a></s:if>
						<s:else><s:property value="month"/></s:else>
					</td> 
					<td  class="down-center-td-width-bg">
						<s:if test="operate==0"><input value="<s:property value="remark"/>" />
							<s:if test="group!=null"><input type="hidden" name="<s:property value="group"/>"/></s:if>
						</s:if>
						<s:else><s:property value="remark"/></s:else>
					</td> 
				 	<td  class="down-center-td-width-bg">
				 		<s:if test="advance&&operate!=2"><img src="${contextPath}/images/del.gif" alt="删除" onclick="del_tr_db(this);" width="16" height="13" border="0" /></s:if>
				 	</td> 
				 	<td style="display: none"><s:property value="id"/></td> 
				</tr>
			</s:iterator>
			</table>
			<table width="100%" border="0" cellpadding="1" cellspacing="1">
			<tr style="padding-top: 20px">
				<td class="bline" colspan="4" align="center">
					<input type="button" name="btn_submit_bak" onclick="build()" id="save" value="确定"/>
					&nbsp;<input type="button" onclick="javascript:window.history.back();" value="返回"/>
				</td>
			</tr>
			</table>
		
			<br/>
		<%-- </s:if> --%>
		<input type="hidden" id="bill_id" name="bill.id" value="<s:property value='bill.id'/>"/>
		<input type="hidden" id="shopId" value="<s:property value='bill.shop.id'/>"/>
		<input type="hidden" id="editData" name="editData"/>
		<input type="hidden" id="newData" name="newData"/>
		<input type="hidden" id="redirectUrl" name="redirectUrl" value="<s:property value='redirectUrl'/>"/>
		</form>
		</div>
	</body>
</html>